/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.heathrow.servicesImpl;

import cz.muni.fi.heathrow.dao.AircraftDAO;
import cz.muni.fi.heathrow.dto.DTOAssembler;
import cz.muni.fi.heathrow.entities.Aircraft;
import cz.muni.fi.heathrowapi.AircraftService;
import cz.muni.fi.heathrowapi.dto.AircraftDTO;
import cz.muni.fi.heathrowapi.HeathrowRoles;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Martin Zitnik
 */
@Service
public class AircraftServiceImpl implements AircraftService {
    
    private AircraftDAO aircraftDao;

    public void setAircraftDao(AircraftDAO aircraftDao) {
        this.aircraftDao = aircraftDao;
    }

    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public void create(AircraftDTO aircraft) {
        if(aircraft == null) 
            throw new IllegalArgumentException("aircraft is null");
        
        Aircraft ac = new Aircraft();
        DTOAssembler.UpdateAircraftDomainObject(aircraft, ac);
        aircraftDao.create(ac);
        DTOAssembler.UpdateAircraftDTO(aircraft, ac);
    }

    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.USER + "')")
    public AircraftDTO get(Long id) {
        if(id == null) 
            throw new IllegalArgumentException("id is null");
        
        return DTOAssembler.CreateAircraftDTO(aircraftDao.get(id));
        
    }

    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public void update(AircraftDTO aircraft){
        if(aircraft == null) 
            throw new IllegalArgumentException("aircraft is null");
        

        Aircraft ac = aircraftDao.get(aircraft.getId());
        DTOAssembler.UpdateAircraftDomainObject(aircraft, ac);

        aircraftDao.update(ac);

    }

    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public void remove(AircraftDTO aircraft) {
        if(aircraft == null) 
            throw new IllegalArgumentException("aircraft is null");        
        
        aircraftDao.remove(aircraftDao.get(aircraft.getId()));        
    }

    
    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.USER + "')")
    public List<AircraftDTO> findAll() {
        List<Aircraft> aircraft;
        List<AircraftDTO> dtoAircraft;
                
        
        aircraft = aircraftDao.findAll();        
        dtoAircraft = DTOAssembler.createAircraftDTOList(aircraft);
        
        return dtoAircraft;
    }
    
    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.USER + "')")
    public List<AircraftDTO> findByCodeWild(String code, int limit) {
        List<Aircraft> aircraft;
        List<AircraftDTO> dtoAircraft;
                
        
        aircraft = aircraftDao.findByCodeWild(code, limit);        
        dtoAircraft = DTOAssembler.createAircraftDTOList(aircraft);
        
        return dtoAircraft;
    }

    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public AircraftDTO findByCode(String code) {
        if(code == null) 
            throw new IllegalArgumentException("code is null");
        
        return DTOAssembler.CreateAircraftDTO(aircraftDao.findByCode(code));
    }
       
    
}
